﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlOpenClosedDate : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadChiNhanh();
            GetClosedDate(ddlChiNhanh.SelectedValue.ToString());
        }
    }
    protected void btOpen_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
          
            int isCheckClosedMonth = dt.ExecuteSQl_int(str, "[ls_isClosedMonth]", CommandType.StoredProcedure, ref error,
                new SqlParameter("@selDate", txtDate.Text),
                new SqlParameter("@siteCode",ddlChiNhanh.SelectedValue.ToString()));
            if (isCheckClosedMonth == 0)
            {
                DateTime date;
                System.Globalization.DateTimeFormatInfo sFormat = new System.Globalization.DateTimeFormatInfo();
                sFormat.ShortDatePattern = "dd/MM/yyyy";
                date = Convert.ToDateTime(txtDate.Text.Trim(), sFormat);

                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_openClosedDate]", ref error,
                    new SqlParameter("@selDate", date.AddDays(-1).ToString("dd/MM/yyyy")),
                    new SqlParameter("@siteCode", ddlChiNhanh.SelectedValue.ToString()));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Hoàn thành mở ngày khóa sổ", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
                }


            }
            else
            {
                uscMsgBox1.AddMessage("Ngày khóa sổ đã thuộc tháng đã khóa sổ", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void ddlChiNhanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetClosedDate(ddlChiNhanh.SelectedValue.ToString());
    }
    void GetClosedDate(string TTPP)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
             txtDate.Text = dt.GetDataDepend(str, "[ls_getClosedDate]", CommandType.StoredProcedure, ref error,
              new SqlParameter("@siteCode", TTPP));
       

        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadChiNhanh()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
            dt.BindDropDownList(db, ddlChiNhanh, "Sitecode", "sitename");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
}